package geez.widget;

import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import static geez.widget.Geez.withStyle;

public class GPopupPanel extends PopupPanel {

    public GPopupPanel(Widget popupContent, boolean hideOnClickOff) {
        super(hideOnClickOff);
        add(popupContent);
    }

    public GPopupPanel id(String id) {
        Geez.withNameAndId(this, id);
        return this;
    }

    public GPopupPanel css(String cssClass, String... optionalClasses) {
        withStyle(this, cssClass, optionalClasses);
        return this;
    }

    public GPopupPanel positionBelowAlignLeft(Widget relevantTo) {
        setPopupPosition(alignLeft(relevantTo), positionBelow(relevantTo));
        return this;
    }

    public GPopupPanel positionBelowAlignRight(Widget relevantTo) {
        setPopupPosition(alignRight(this, relevantTo), positionBelow(relevantTo));
        return this;
    }

    private static int positionBelow(Widget relevantTo) {
        return relevantTo.getAbsoluteTop() + relevantTo.getOffsetHeight();
    }

    private static int alignLeft(Widget relevantTo) {
        return relevantTo.getAbsoluteLeft();
    }

    private static int alignRight(Widget positioningWidget, Widget relevantTo) {
        return (relevantTo.getAbsoluteLeft() + relevantTo.getOffsetWidth()) - positioningWidget.getOffsetWidth();
    }

}
